package com.waidi.clothespad.ui;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.waidi.clothespad.R;
import com.waidi.clothespad.common.Config;
import com.waidi.clothespad.common.DpsInterface;

import com.waidi.clothespad.common.HttpGetThread;
import com.waidi.clothespad.common.MessageToUi;
import com.waidi.clothespad.common.MyToast;
import com.waidi.clothespad.mqttclient.MyMqttClient;

import org.json.JSONObject;

import java.net.URLEncoder;

/**
 * Created by Administrator on 2019/6/26.
 */

public class QrCodeSwitchDialog extends Dialog{
    private View view;
    private static final String TAG = "QrCodeSwitchDialog";
    private Context context;
    private Handler handler0;

    private  QrCodeSwitchDialogHandler selfHandler;
    private TextView tV_qrCode_id;
    private TextView tV_order_id;
    private TextView tV_style_id;
    private TextView tV_style_name;

    private TextView tV_color;
    private TextView tV_size;

    private TextView tV_line_no;
    private TextView tV_num;







    private Button bt_ok;
    private Button bt_cancle;



    public String cutPlanId ;

    public static String title0 = "请刷物料卡...";
    public static String partName = "";
    public static String partCode = "";
    private static JSONObject dataJson = null;


    public QrCodeSwitchDialog(@NonNull Context context,Handler handlerin) {
        super(context);
        this.context = context;
        this.handler0 = handlerin;
        selfHandler = new QrCodeSwitchDialogHandler();
        init();
    }
    public QrCodeSwitchDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //init();
    }

    private void init() {

        View view = LayoutInflater.from(context).inflate(R.layout.layer_qrcode_switch_info ,null);
        tV_qrCode_id = (TextView) view.findViewById(R.id.tv_qrcode);
        tV_order_id = (TextView) view.findViewById(R.id.tv_order_id);
        tV_style_id = (TextView) view.findViewById(R.id.tv_style_id);

        tV_style_name = (TextView) view.findViewById(R.id.tv_style_name);

        tV_color = (TextView) view.findViewById(R.id.tv_color);
        tV_size = (TextView) view.findViewById(R.id.tv_size);
        tV_line_no = (TextView) view.findViewById(R.id.tv_line_no);
        tV_num = (TextView) view.findViewById(R.id.tv_num);


        bt_ok = (Button) view.findViewById(R.id.bt_ok);
        bt_cancle = (Button) view.findViewById(R.id.bt_cancle);




        bt_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                dismiss();
            }
        });

        bt_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {

            try
            {

                if(MyMqttClient.myClientConn)
                {
                    MyMqttClient.switchSyncToDg(1,dataJson);
                    dismiss();
                    MyToast.showToast(context,"切换指令已发送到吊挂,等待吊挂软件执行...");

                }
                else
                {
                    MyToast.showToast(context,"MQTT服务未连接...");
                }

            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            }
        });

        setContentView(view);
        this.getWindow().setDimAmount(Config.transparency);
        Window dialogWindow = getWindow();
        WindowManager manager = ((Activity) context).getWindowManager();
        WindowManager.LayoutParams params = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        dialogWindow.setGravity(Gravity.CENTER);//设置对话框位置
        Display d = manager.getDefaultDisplay(); // 获取屏幕宽、高度
        params.width = (int) (d.getWidth() * 0.45); // 宽度设置为屏幕的0.65，根据实际情况调整
        params.height = (int) (d.getHeight() * 0.75); // 宽度设置为屏幕的0.65，根据实际情况调整
        dialogWindow.setAttributes(params);
    }

    public void iniData()
    {

        //使dialog失去焦点 使键盘事件能够一直停留在主Activity 上
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        this.setCanceledOnTouchOutside(true);
    }





    public void refushData(String qrCode,String orderId,String styleID,String styleName,
                           String colorId,String sizeId,String lineNum,String hanger,
                           String orderNum)
    {

        tV_qrCode_id.setText(qrCode);
        tV_order_id.setText(orderId);
        tV_style_id.setText(styleID);
        tV_style_name.setText(styleName);
        tV_color .setText(colorId);
        tV_size.setText(sizeId);
        tV_line_no.setText(lineNum);
        tV_num .setText(orderNum);


        //使dialog失去焦点
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        this.setCanceledOnTouchOutside(true);


    }

    public void refushData(String qrcode,JSONObject jsonObjData)
    {
        dataJson = jsonObjData;
        try
        {
            //      "po":"D190625-0006/6",
            //      "qty":500,
            //      "agreedDate":1562601600000,
            //      "comfort":"合体",
            //       "remark":"",
            //        "creator":"yang",
            //        "styNum":"LS0612",
            //        "styName":"利世",
            //        "isUrgent":0,
            //        "typeNameInfo":"[{"name":"88A","qty":500}]",
            //        "csNum":"10000563",
            //        "csName":"LilyGuan",
            //        "orderType":2,
            //        "lineNum":"1101",
            //        "mainMaterial":"面料",
            //        "color":"紫色",
            //        "typeName":"85A",
            //        "typeQty":500
            String orderId = jsonObjData.optString("po");
            String styleId = jsonObjData.optString("styNum");
            String styleName  = jsonObjData.optString("styName");
            String colorId =  jsonObjData.optString("color");
            String sizeId = jsonObjData.optString("typeName");
            String lineNum = jsonObjData.optString("lineNum");
            String orderNum = jsonObjData.optString("typeQty");

            tV_qrCode_id.setText(qrcode);
            tV_order_id.setText(orderId);
            tV_style_id.setText(styleId);
            tV_style_name.setText(styleName);
            tV_color .setText(colorId);
            tV_size.setText(sizeId);
            tV_line_no.setText(lineNum);
            tV_num .setText(orderNum);

        }
        catch (Exception e)
        {

        }




        //使dialog失去焦点
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        this.setCanceledOnTouchOutside(true);


    }


/*    String barcode ="";
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        String code;
        Log.i(TAG,"##"+event.toString());
        {
            if (event.getAction() != KeyEvent.ACTION_DOWN)
            {
                Log.i(TAG, "dispatchKeyEvent: " + event.toString());
                char pressedKey = (char) event.getUnicodeChar();
                barcode += pressedKey;
            }
            if ((event.getAction() == KeyEvent.ACTION_DOWN)
                    && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                Toast.makeText(context,
                        "barcode--->>" + barcode, Toast.LENGTH_SHORT)
                        .show();

                // qrEditText.setText(barcode);
                //MyToast.showToast(context,barcode);
                Log.i(TAG, "BARCODE:" + barcode);
                code = barcode;
                tV_qrCode_id.setText(barcode);
               // new MessageToUi().send(900, "qrcode", code, Config.fragShishishuju.handler);
                barcode = "";
            }


        }
        return super.dispatchKeyEvent(event);
    }*/


    //自定义handler类
    class QrCodeSwitchDialogHandler extends Handler
    {
        @Override
        //接收别的线程的信息并处理
        public void handleMessage(Message msg) {
            Log.i(TAG,Integer.toString(msg.arg1));
            Bundle bundle;
            try {
                    switch (msg.arg1)
                    {
                        case 0:
                        {

                        }
                        break;
                        case 1:
                        {


                        }
                        break;
                        case 2:

                            break;
                        case 23:
                        {
                            Bundle bundle23=msg.getData();
                            String exception = bundle23.getString("exception");
                            MyToast.showToast(context,exception);
                        }
                        break;
                        case 444:
                        {
                            Bundle bundle444=msg.getData();
                            String info = bundle444.getString("exception");
                            MyToast.showToast(context,info);

                        }
                        break;

                        default:
                            break;
                }
            }catch (Exception e)
            {
                e.printStackTrace();
                MyToast.showToast(context,e.toString());
            }
        }
    }

}
